package com.bang.concurrency.example.atomic;

import com.bang.concurrency.annotations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Auther: Bang
 * @Date: 2019/9/29 22:21
 * @Description: AtomicIntegerFieldUpdater
 */
@Slf4j
@ThreadSafe
public class AtomicExample5 {


    private static AtomicIntegerFieldUpdater<AtomicExample5> updater =
            AtomicIntegerFieldUpdater.newUpdater(AtomicExample5.class, "count");

    @Getter
    private volatile int count = 100;

    public static void main(String[] args) throws InterruptedException {
        AtomicExample5 example5 = new AtomicExample5();
        if (updater.compareAndSet(example5, 120, 100)) {
            log.info("update success 1, {}", example5.getCount());
        }
        if (updater.compareAndSet(example5, 100, 120)) {
            log.info("update success 2, {}", example5.getCount()); // 120
        } else {
            log.info("update failed, {}", example5.getCount());
        }
    }


}
